package com.iqi.onigao.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.thread.ThreadUtil;
import net.IClient;

public class ThreadExecutor {
	static ExecutorService[] executor = new ExecutorService[8];

	static ScheduledExecutorService service = Executors.newScheduledThreadPool(8); 
	
	static {
		for (int i = 0; i < executor.length; i++) {
			executor[i] = ThreadUtil.newSingleExecutor();
		}
	}

	public static ExecutorService getThreadExecutor(int index) {
		return executor[index];
	}

	public static void execute(IClient session,Runnable command) {
		int index = (int) (session.getId() % executor.length);
		executor[index].execute(command);
	}
	
	public static ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long delay,TimeUnit unit) {
		return service.schedule(command, delay, unit);
	}
}
